<template>
  <el-dialog width="900px" :title="data?'修改表 '+data.name:'创建表'" :visible.sync="is_visible" @close="on_close">
    <el-form size="small" label-width="60px" style="margin-right: 30px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="表名:" :error="String(errors.name?errors.name:'')">
            <el-input
              :disabled="data"
              v-model="form_data.name">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="别名:" :error="String(errors.alias?errors.alias:'')">
            <el-input v-model="form_data.alias"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="描述:" :error="String(errors.readme?errors.readme:'')">
        <el-input v-model="form_data.readme" type="textarea"></el-input>
      </el-form-item>
    </el-form>
    
    <el-form label-width="60px" style="margin-right: 30px">
      <el-form-item>
        <el-button size="small" style="width: 100%" type="primary" :loading="loading" @click="on_submit()">确定</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import master from "@/api";
import { master_s } from "@/api";

export default {
  name: "change-table",
  data() {
    return {
      is_visible: true,
      loading: false,
      form_data: {
        name: "",
        alias: "",
        readme: "",
      },
      errors: {}
    };
  },
  props: ["is_create", "data"],
  mounted() {
    if (!this.is_create) {
      this.form_data = this.data;
    }   
    // this.data.fields.push({"id":121,"name":"xxxxx","alias":null,"readme":"","type":0,"is_multi":false,"required":true})
  },
  methods: {
    on_close() {
      this.$el.parentNode.removeChild(this.$el);
      this.$destroy();
    },
    on_submit() {
      this.loading = true;
      if (this.is_create) {
        master_s
          .post("mgmt/table/", this.form_data)
          .then(response => {
            this.loading = false;
            this.$message.success("添加成功");
            this.errors = {};
            this.$emit("add_table", response.data);
            this.on_close();
          })
          .catch(error => {
            this.loading = false;
            this.errors = error.response.data;
          });
      } else {
        master_s
          .put("mgmt/table/" + this.form_data.name + '/', this.form_data)
          .then(response => {
            this.loading = false;
            this.$message.success("修改成功");
            this.errors = {};
            // this.$emit("add_table", response.data);
            this.on_close();
          })
          .catch(error => {
            this.loading = false;
            this.errors = error.response.data;
          });
      }
    }
  }
};
</script>

<style scoped>
</style>
